// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainManage from "../../framework/mainManage";
import gameData from "../gameData";
import GameConfig from "../../framework/GameConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class threadView extends cc.Component {

    @property(cc.Node)
    pageList : cc.Node[] = [];

    threadType = 0;

    curPage = 0;

    pageMax = 5;

    curThread = 0;

    lockData = null;

    curThreadList = [];

    changePage = false;

    callback = null;

    index = 0;

    onLoad () {
        let index = this.lockData.indexOf(0);
        if( index > -1){
            this.lockData[index] = 1;
            if(this.threadType == 0){
                gameData.skinThreadLockData[this.index] = this.lockData;
                gameData.saveGetSkinThreadLockData();
            }else{
                gameData.eggThreadLockData[this.index] = this.lockData;
                gameData.saveGetEggThreadLockData();
            }
        }
        this.createThreadView();
    }

    setCallBack(type,curThread,lockData,threadList,callback){

        this.threadType = type;

        //设置当前页为继续完成页
        this.curThreadList = threadList;

        this.callback = callback;

        this.index = lockData;

        if(this.threadType == 0){
            this.lockData = gameData.skinThreadLockData[lockData];
        }else{
            this.lockData = gameData.eggThreadLockData[lockData];
        }

        this.curThread = curThread;

        this.pageMax = Math.ceil(curThread / 2);

        if(this.lockData.indexOf(0) > -1){
            this.curPage = Math.ceil((this.lockData.indexOf(0) + 1) / 2) - 1;
        }else{
            this.curPage = Math.ceil(this.lockData.length / 2) - 1;
        }
    }

    createThreadView(){
        if(this.pageMax <= 1){
            this.node.getChildByName("left").active = false;
            this.node.getChildByName("right").active = false
        }else{
            if(this.curPage == 0){
                this.node.getChildByName("left").active = false;
            }else{
                this.node.getChildByName("left").active = true;
            }

            if(this.curPage + 1 >= this.pageMax ){
                this.node.getChildByName("right").active = false;
            }else{
                this.node.getChildByName("right").active = true;
            }
        }

        for(let i in this.pageList) {
            let child = this.pageList[i].children;
            for (let j in child) {
                let index = -2 + this.curPage * 2 + 2 * parseInt(i) + (parseInt(j) + 1);
                if(index > this.curThread){
                    child[j].active = false;
                }else{
                    child[j].getChildByName("name").getComponent(cc.Label).string = "线索" + index;
                    child[j].getChildByName("title").getComponent(cc.Label).string = this.curThreadList[index - 1];
                    if(this.lockData[index - 1] == 0){
                        child[j].getChildByName("btn1").getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
                        child[j].getChildByName("btn1").active = true;
                    }else{
                        child[j].getChildByName("btn1").active = false;
                    }
                }
            }
        }
    }

    closeUnlockView(event,data){
        this.node.destroy();
        if(this.callback != null){
            this.callback();
        }
    }

    seeThread(event,data){
        event.target.active = false;
        this.lockData[parseInt(data) - 1] = 1;
        if(this.threadType == 0){
            gameData.skinThreadLockData[this.index] = this.lockData;
            gameData.saveGetSkinThreadLockData();
        }else{
            gameData.eggThreadLockData[this.index] = this.lockData;
            gameData.saveGetEggThreadLockData();
        }
    }

    pageDown(event,data){
        if(this.changePage) return;
        if(this.curPage <= 0){
            console.log("到头了");
            return;
        }
        this.changePage = true;
        this.curPage--;

        if(this.pageMax <= 1){
            this.node.getChildByName("left").active = false;
            this.node.getChildByName("right").active = false
        }else{
            if(this.curPage == 0){
                this.node.getChildByName("left").active = false;
            }else{
                this.node.getChildByName("left").active = true;
            }

            if(this.curPage + 1 >= this.pageMax ){
                this.node.getChildByName("right").active = false;
            }else{
                this.node.getChildByName("right").active = true;
            }
        }

        cc.tween(this.pageList[0]).by(0.8,{
            position : cc.v2(750,0)
        }).start();
        cc.tween(this.pageList[1]).by(0.8,{
            position : cc.v2(750,0)
        }).call(()=>{
            this.pageList[2].x = -750;
            let child = this.pageList[2].children;
            for (let i in child) {
                let index = (2 * (this.curPage - 1) + parseInt(i) + 1);
                if(index > this.curThread){
                    child[i].active = false;
                }else{
                    child[i].active = true;
                    child[i].getChildByName("name").getComponent(cc.Label).string = "线索" + index;
                    child[i].getChildByName("title").getComponent(cc.Label).string = this.curThreadList[index - 1];
                    if(this.lockData[index - 1] == 0){
                        child[i].getChildByName("btn1").getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
                        child[i].getChildByName("btn1").active = true;
                    }else{
                        child[i].getChildByName("btn1").active = false;
                    }
                }
            }
            this.pageList.unshift(this.pageList.pop());
            this.changePage = false;
        }).start();
        console.log("左翻页");
    }

    pageUp(event,data){
        if(this.changePage) return;
        if(this.curPage >= this.pageMax - 1){
            console.log("到头了");
            return;
        }
        this.changePage = true;
        this.curPage++;

        if(this.pageMax <= 1){
            this.node.getChildByName("left").active = false;
            this.node.getChildByName("right").active = false
        }else{
            if(this.curPage == 0){
                this.node.getChildByName("left").active = false;
            }else{
                this.node.getChildByName("left").active = true;
            }

            if(this.curPage + 1 >= this.pageMax ){
                this.node.getChildByName("right").active = false;
            }else{
                this.node.getChildByName("right").active = true;
            }
        }

        cc.tween(this.pageList[1]).by(0.8,{
            position : cc.v2(-750,0)
        }).start();
        cc.tween(this.pageList[2]).by(0.8,{
            position : cc.v2(-750,0)
        }).call(()=>{
            this.pageList[0].x = 750;
            let child = this.pageList[0].children;
            for (let i in child) {
                let index = (2 * (this.curPage + 1) + parseInt(i) + 1);
                if(index > this.curThread){
                    child[i].active = false;
                }else{
                    child[i].active = true;
                    child[i].getChildByName("name").getComponent(cc.Label).string = "线索" + index;
                    child[i].getChildByName("title").getComponent(cc.Label).string = this.curThreadList[index - 1];
                    if(this.lockData[index - 1] == 0){
                        child[i].getChildByName("btn1").getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
                        child[i].getChildByName("btn1").active = true;
                    }else{
                        child[i].getChildByName("btn1").active = false;
                    }
                }
            }
            this.pageList.push(this.pageList.shift());
            this.changePage = false;
        }).start();
        console.log("右翻页");
    }
}
